{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color='blue', size=6>Matplotlib</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Origin Notes-CSDN](https://blog.csdn.net/han_xiaoyang/article/details/49797143)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 格式设置\n",
    "## 全局参数设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams\n",
    "# rcParams['axes.edgecolor']='white'\n",
    "# rcParams['xtick.color']='black'\n",
    "# rcParams['ytick.color']='black'\n",
    "# rcParams['axes.labelcolor']='black'\n",
    "# rcParams['text.color']='black'\n",
    "# rcParams['figure.edgecolor']=(1, 1, 1, 1)\n",
    "rcParams['figure.facecolor']=(1, 1, 1, 1)\n",
    "rcParams['font.family']='simhei'\n",
    "rcParams['font.size']=13\n",
    "\n",
    "#获取全部带font字样的参数名及value\n",
    "list=[(param, value) for param, value in plt.rcParams.items() if 'font' in param]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 图像元素设置\n",
    "### 坐标轴范围设置\n",
    "```python\n",
    "#方法1\n",
    "plt.axis([x_start,x_end,y_start,y_end])\n",
    "\n",
    "#方法2\n",
    "table.plot(yticks=np.linspace(0.4,1.2,9),xticks=range(1880,2020,10),figsize=(10,8))   #xticks需要传入一个list\n",
    "\n",
    "#range函数\n",
    "xticks=range(1880,2020,10)  #step=10，不包含2020\n",
    "xticks=range(1880,2021,10)  #需要2020时→2020+1\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 图例设置\n",
    "[`plt.legend()`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 子区域绘图\n",
    "## subplots方法\n",
    "```python\n",
    "#返回Figure对象、子区域对象组成的list对象\n",
    "fig,axes=plt.subplots(2,1,figsize=(10,8))\n",
    "\n",
    "#在各个子区域绘图：ax=axes[i]\n",
    "letter_prop['M'].plot(kind='bar',rot=0,ax=axes[0],title='Male')\n",
    "letter_prop['F'].plot(kind='bar',rot=0,ax=axes[1],title='Female',legend=False)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 调整subplots子区域之间的间距\n",
    "1. `fig.tight_layout()`：自动排版\n",
    "2. `subplots_adjust`：精确设置间距\n",
    "```python\n",
    "plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)<br>\n",
    "left  = 0.125  # the left side of the subplots of the figure\n",
    "right = 0.9    # the right side of the subplots of the figure\n",
    "bottom = 0.1   # the bottom of the subplots of the figure\n",
    "top = 0.9      # the top of the subplots of the figure\n",
    "wspace = 0.2   # the amount of width reserved for blank space between subplots\n",
    "hspace = 0.2   # the amount of height reserved for white space between subplots\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 子区域共享轴\n",
    "共享一个y轴，防止两子区域，轴范围不一样，无法比较\n",
    "```python\n",
    "ax21=fig2.add_subplot(141)\n",
    "...\n",
    "ax22 = fig2.add_subplot(142)\n",
    "```\n",
    "![不共享](images/notshared.jpg)\n",
    "\n",
    "**`sharey=ax21`**<br>\n",
    "`sharey sharex`\n",
    "```python\n",
    "ax21=fig2.add_subplot(141)\n",
    "...\n",
    "ax22 = fig2.add_subplot(142,sharey=ax21)\n",
    "...\n",
    "\n",
    "plt.setp(ax22.get_yticklabels(),visible=True)  #设置子图坐标轴刻度标签是否课件\n",
    "plt.setp(ax23.get_yticklabels(),visible=True)  #两句写在一起，放在最后\n",
    "```\n",
    "\n",
    "![共享](images/shared.jpg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
